﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MediaGarden.Pipeline;
using ImageResizer;
using MediaGarden.Services;
using System.Drawing;
using System.IO;

namespace MediaGarden.ImageResizer.Filters
{
    public class ImageResizingDisplayFilter : IMediaDisplayFilter
    {
        private readonly IMediaGardenService _gardenService;

        public ImageResizingDisplayFilter(IMediaGardenService gardenService)
        {
            _gardenService = gardenService;
        }

        public void MediaDisplaying(MediaDisplayContext context)
        {
            // Perform resize? Must have both source and target sizes
            if (context.Options.OriginalWidth > 0 && context.Options.OriginalHeight > 0
                && context.Options.TargetWidth > 0 && context.Options.TargetHeight > 0)
            {
                var fileExt = context.MediaItem.MediaSource.FileExtension();
                if (ImageBuilder.Current.GetSupportedFileExtensions().Contains(fileExt))
                {
                    // TODO: Allow setting defaults in Config, and passing values in thru media shape Options

                    // TODO: We could fire off another event to customise the imagebuilder pipeline;
                    // so additional modules can add features to ImageBuilder

                    var settings = new ResizeSettings()
                    {
                        
                        Width = context.Options.TargetWidth,
                        Height = context.Options.TargetHeight,
                        Scale = ScaleMode.DownscaleOnly,
                        Stretch = StretchMode.Proportionally,
                        BackgroundColor = Color.Black
                    };

                    // TODO: Allow a cropping rectangle and other sizing options (and at UI end)

                    if (context.Options.Resize) settings.Stretch = StretchMode.Proportionally;
                    if (context.Options.Crop) settings.CropMode = CropMode.Auto;
                    
                    var location = context.Options.SourceFilePath;
                    var final = ImageBuilder.Current.GetFinalSize(new Size(context.Options.OriginalWidth, context.Options.OriginalHeight), settings);

                    // TODO: Allow padding control and other features!

                    var appendix = final.Width.ToString() + "x" + final.Height.ToString() + "."+fileExt;
                    var withoutExt = location.Substring(0, location.Length - 1 - fileExt.Length);
                    var withoutExtUrl = context.Options.SourceUrl.Substring(0, context.Options.SourceUrl.Length - 1 - fileExt.Length);
                    if (!Directory.Exists(withoutExt))
                        Directory.CreateDirectory(withoutExt);
                    context.Options.ActualFilePath = withoutExt + "\\" + appendix;
                    context.Options.ActualUrl = withoutExtUrl + "/"+appendix;
                    // Need to create image?
                    var gen = !File.Exists(context.Options.ActualFilePath);
                    if (gen)
                    {
                        // Compare file dates
                        var infoSource = File.GetLastWriteTime(location);
                        var infoTarget = File.GetLastWriteTime(context.Options.ActualFilePath);
                        if (infoTarget >= infoSource)
                        {
                            gen = false;
                        }
                    }
                    if (gen)
                    {
                        var finalloc = ImageBuilder.Current.Build(location, context.Options.ActualFilePath, settings, false, false);
                    }
                }
            }
        }

        public void MediaDisplayed(MediaDisplayContext context)
        {
        }
    }
}